package com.letv.business.flow.PlayFlowState;

import android.text.TextUtils;

import com.ledim.app.LeDimApplication;
import com.letv.android.young.client.R;
import com.letv.business.flow.PlayFlowEvents.PlayUrlPrepareEvent;
import com.letv.business.flow.album.AlbumPlayFlowObservable;
import com.letv.business.flow.album.listener.LoadLayoutFragmentListener;
import com.letv.business.flow.album.model.AlbumPlayInfo;
import com.letv.core.bean.AlbumPayInfoBean;
import com.letv.core.bean.VideoBean;
import com.letv.core.bean.VideoFileBean;
import com.letv.core.constant.PlayConstant;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.TipUtils;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by jipengfei on 2016/10/31.
 */
public class PlayAuthState extends BasePlayState{

    @Override
    protected void injectDependencies() {
        LeDimApplication.getComponentInstance().inject(this);
    }


    @Override
    public void onStateApplied() {
        super.onStateApplied();

        LogInfo.log("sguotao", "进入Auth State");

        mPlayFlow = getProvider().providPlayFlow();

        checkCanPlay();
    }

    @Override
    public void onStateLeft() {
        super.onStateLeft();

    }

    @Subscribe
    public void onPlayUrlPrepareEvent(PlayUrlPrepareEvent playUrlPrepareEvent) {
//        playUrlPrepareEvent.requestRealPlayUrl();
        fireEvent(PlayFlowEvents.Play_UrlPrepare);
    }

    @Subscribe
    public void onPlayErrorEvent(PlayErrorState playErrorState) {
        fireEvent(PlayFlowEvents.Play_Error);
    }

    /**
     * 判读视频是否可以播放
     */
    protected void checkCanPlay() {
        AlbumPayInfoBean mPayInfo = mPlayFlow.getPayInfo();
        AlbumPlayInfo  mPlayInfo = mPlayFlow.getPlayInfo();
        VideoBean mCurrentPlayingVideo = mPlayFlow.getPlayingVideo();
        VideoFileBean videoFile = mPlayFlow.getmVideoFile();
        LoadLayoutFragmentListener mLoadListener = mPlayFlow.getLoadListener();


        mPlayInfo.canPlayConsumeTime =
                System.currentTimeMillis() - mPlayInfo.canPlayConsumeTime;

        if (mCurrentPlayingVideo == null) {
            fireEvent(PlayFlowEvents.Play_Error);
            return;
        }


        if (mPayInfo == null) {
            mPayInfo = new AlbumPayInfoBean();
            mPayInfo.setStatus(mCurrentPlayingVideo.canPlay() ? 1 : 0);
        }
        //            mTrailListener.setTicketSize(mPayInfo.getTicketSize());
        //            mTrailListener.setChargetype(mPayInfo.getChargetype());

        // 播放鉴权
        if (!mCurrentPlayingVideo.canPlay()) {
//            mCanToPip = false;
            authentication();
            fireEvent(PlayFlowEvents.Play_Error);
            return;
        }

        // IP被屏蔽
        if (!videoFile.isIpEnable()) {
            ipDisable(videoFile);

//            // 海外屏蔽 ，上传错误码
//            DataStatistics.getInstance()
//                    .sendErrorInfo(mPlayFlow.getContext(), "0", "0", LetvErrorCode.VIDEO_FOREIGN_SHIELD, null, hull
//                            .getReportErrorString(), null, null, null, null);
//            mCanToPip = false;

            fireEvent(PlayFlowEvents.Play_Error);

            return;
        }

        // 判断付费信息
        //            if (mPayInfo.getStatus() == 0) {
        //                // 不能播放,提示信息
        //                showVipTrailStart();
        //            }

        // 判断视频文件信息,次元暂时无此本地下载逻辑,fix me
//        if (mIsLocalSuccess && mIsDownloadFile) {
//            // 本地文件，并且加载成功，返回
//            return;
//        }

        // 可以播放
        if (mLoadListener.isLoadingShow()) {
            mLoadListener.loadingVideo(mCurrentPlayingVideo.getNameCn());
        }
        //            checkWoFree();
        //请求真实最终地址
        fireEvent(PlayFlowEvents.Play_UrlPrepare);

    }

    /**
     * 鉴权
     *
     * @return
     */
    private void authentication() {
        // 不能播放.如果jump为1，则根据jumptype做相应提示，如果jump为0，则直接提示手机不能观看
        VideoBean mCurrentPlayingVideo =  mPlayFlow.getPlayingVideo();
        LoadLayoutFragmentListener mLoadListener = mPlayFlow.getLoadListener();

        if (mCurrentPlayingVideo.needJump()) {
            String type = mCurrentPlayingVideo.getJumptype();
            mPlayFlow.addPlayInfo("鉴权失败，需要外跳，jumpType", type);
            if (TextUtils.equals(type, PlayConstant.Authentication.WEB)) {
                // 不提供网页链接按钮
                mLoadListener.jumpError(2);
            } else if (TextUtils.equals(type, PlayConstant.Authentication.WEB_JUMP)) {
                // 提供网页链接按钮
                mLoadListener.jumpError(1);
            } else if (TextUtils.equals(type, PlayConstant.Authentication.TV_JUMP)) {
                // 使用TV投屏观看
                String spTitle =
                        TipUtils.getTipMessage(mPlayFlow.getContext(), "100052", mPlayFlow.getContext().getString(R.string.screen_projection_jump));
                String spMsg =
                        TipUtils.getTipMessage(mPlayFlow.getContext(), "100055", mPlayFlow.getContext().getString(R.string.screen_projection));
                mLoadListener.jumpError(spTitle, spMsg, true);
            } else if (TextUtils.equals(type, PlayConstant.Authentication.NO_COPYRIGHT) ||
                    TextUtils.isEmpty(type)) {
                // 三端均无版权
                mLoadListener.jumpError(0);
            }
            return;
        }

        // 下线视频
        mPlayFlow.addPlayInfo("鉴权失败", "下线视频");
        mLoadListener.jumpError(0);
    }

    /**
     * ip受限
     *
     * @param videoFile
     */
    private void ipDisable(VideoFileBean videoFile) {
        LoadLayoutFragmentListener mLoadListener = mPlayFlow.getLoadListener();
        AlbumPlayFlowObservable mObservable = mPlayFlow.getObserver();

        mPlayFlow.addPlayInfo("ip被屏蔽", "");
        if (TextUtils.equals("CN", videoFile.getCountry())) {
            // 大陆ip受限
            mLoadListener.ipError(TipUtils.getTipMessage(mPlayFlow.getContext(), "0012", R.string.cn_ip_error));
            mObservable.notifyObservers(new AlbumPlayFlowObservable.PlayErrorCodeNotify(PlayConstant.PlayErrCode.OVERSEA_IP_LIMITED_CN, true));
        } else if (TextUtils.equals("HK", videoFile.getCountry())) {
            // 香港ip受限
            mLoadListener.ipError(TipUtils.getTipMessage(mPlayFlow.getContext(), "100030", R.string.hk_ip_error));
            mObservable.notifyObservers(new AlbumPlayFlowObservable.PlayErrorCodeNotify(PlayConstant.PlayErrCode.OVERSEA_IP_LIMITED_HONGKONG, true));
        } else {
            // 海外ip受限
            mLoadListener.ipError(TipUtils.getTipMessage(mPlayFlow.getContext(), "100031", R.string.other_ip_error));
            mObservable.notifyObservers(new AlbumPlayFlowObservable.PlayErrorCodeNotify(PlayConstant.PlayErrCode.OVERSEA_IP_LIMITED_HONGKONG, true));
        }
    }
}
